package lang

import (
	"bytes"
	"encoding/gob"
	"unsafe"
)

func InterfaceToByte(v interface{}) ([]byte, error) {
	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
	err := enc.Encode(v)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

func ByteToInterface(b []byte, v interface{}) error {
	d := gob.NewDecoder(bytes.NewReader(b))
	return d.Decode(v)
}

func StringToByte(s string) []byte {
	x := (*[2]uintptr)(unsafe.Pointer(&s))
	h := [3]uintptr{x[0], x[1], x[1]}
	return *(*[]byte)(unsafe.Pointer(&h))
	return nil
}

func ByteToString(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}
